/**
 * 
 */
package net.intellect.force;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sforce.soap.enterprise.sobject.Holiday__c;
import com.sforce.soap.enterprise.sobject.SObject;

/**
 * Defines methods .
 * @author Intellect International (TRF)
 */
public class Utilities {
	
	protected static Logger logger = Logger.getLogger("utitlies");
	
	/**
	 * Gests holidays from data base
	 * @param enterpriseId
	 */
	public static Map<String, HashMap<Integer, Holiday__c>> getHolidayMap(String enterpriseId) {
		List<SObject> holidayList = new ArrayList<SObject>();
		Map<String, HashMap<Integer, Holiday__c>> holidayMap = new HashMap<String, HashMap<Integer, Holiday__c>>();;
		try {
			holidayList = ClientSalesforce.query("Select h.Name, h.Month__c, h.Id, h.Day__c From Holiday__c h "+
					"where h.Enterprise_Account__c = '" + enterpriseId + "'");
		} catch(Exception e) { logger.log(Level.INFO, "COLLECT - No holydays found. " + e); }
		// holidays
		// iterates over holiday list
		for (int i=0; i<holidayList.size(); i++) {
			Holiday__c holiday = (Holiday__c) holidayList.get(i);
			// Integer month = new Integer(holiday.getMonth__c());
			 Integer day = new Integer(holiday.getDay__c());
			 if (!holidayMap.containsKey(holiday.getMonth__c())) holidayMap.put(holiday.getMonth__c(), new HashMap<Integer, Holiday__c>());
			 if (!holidayMap.get(holiday.getMonth__c()).containsKey(day)) holidayMap.get(holiday.getMonth__c()).put(day, holiday);
		}
		return holidayMap;
	}

	/**
	 * Verify if giving date is a holiday or not
	 * @param date
	 * @return true if giving date is a holiday. False if not. 
	 */
	public static boolean isHoliday(Date date, Map<String, HashMap<Integer, Holiday__c>> holidayMap) {
		Calendar myCal = Calendar.getInstance();
		boolean isHoliday = false;
		String month = null;

		myCal.setTime(date);
		switch (myCal.get(Calendar.MONTH)) {
			case 0: month = "January";
			case 1: month = "February";
			case 2: month = "March";
			case 3: month = "April";
			case 4: month = "May";
			case 5: month = "June";
			case 6: month = "July";
			case 7: month = "August";
			case 8: month = "September";
			case 9: month = "October";
			case 10: month = "November";
			case 11: month = "December";
			default: break;
		}
		
		if (holidayMap.containsKey(myCal.get(Calendar.MONTH)+1))
			if (holidayMap.get(month).containsKey(myCal.get(Calendar.DAY_OF_MONTH))) isHoliday = true;
		return isHoliday;
	}
	
	/**
	 * Gets date on a format is ok for salesforce
	 * @param date
	 * @return string 
	 */
	public static String getSalesforceDate(Date date){
		SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
		return formato.format(date);		
	}
	
	public static String getSalesforceDateTime(Date fecha){
		SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd'T00:00:00Z'");
		return formato.format(fecha);
	}

	/**
	 * Retruns the very next day from a giving date
	 * @param fecha1
	 * @return Date obj
	 */
	public static Date getNexCalendarDay(Date fecha1) {
		Calendar calendario = Calendar.getInstance();
		calendario.setTimeInMillis(fecha1.getTime());
		calendario.add(Calendar.DATE, 1);		
		return calendario.getTime();
	}

	
	/**
	 * Returns a date with no specified time
	 * @param fecha1
	 * @return Date obj
	 */
	public static Date getTimelessDate(Date date) {
		Calendar calendario = Calendar.getInstance();
		calendario.setTimeInMillis(date.getTime());
		calendario.set(Calendar.HOUR_OF_DAY,0);
		calendario.set(Calendar.MINUTE,0);
		calendario.set(Calendar.SECOND,0);
		calendario.set(Calendar.MILLISECOND,0);
		return calendario.getTime();
	}
}
